<?php
require_once("../includes/config.inc.php");
validate_admin();
$userId = $_REQUEST['userId'];
if($_POST)
{
	@extract($_POST);
	// Edit Code
	$noerror=true;
	if(isFieldDuplicate($tableName='tbl_user',$fieldName='email',$filedValue=$email,$autoIdName='userId',$autoIdValue=$userId,$isNullAllowed=false)) 
	{	
		$_SESSION['sessMsg'] = " Sorry, this Email ".$email." already exists. Please choose a different Email . ";
		$noerror=false;
	}
	if($noerror)
	{	
		if($userId)
		{
			$sqlEdit = " update tbl_user set
			courseId = '$courseId',
			firstName = '$firstName',
			lastName = '$lastName',
			email = '$email',
			password = '$password',
			confirmPassword = '$confirmPassword',
			description = '$description'
			where userId ='$userId'";
			executeQuery($sqlEdit);
			
			$_SESSION['sessMsg'] = "User Detail Edited Successfully. ";
			header("location:userManager.php");
			exit();
		}
		else
		{
			// Add Code
			$sqlPA = " insert into tbl_user 
			(
				courseId,
				firstName,
				lastName,
				email,
				password,
				confirmPassword,				
				description,
				regDate,
				status
			)
			values
			(
				'$courseId',
				'$firstName',
				'$lastName',
				'$email',
				'$password',
				'$confirmPassword',
				'$description',
				CURDATE(),
				'1'
			)
			";
			executeQuery($sqlPA );			
			$_SESSION['sessMsg'] = " New User Added Successfully. ";
			header("location: userManager.php");
			exit();
		}
	}		
}
			
if($userId)
{
	$sqlPD = " select * from tbl_user where userId = '$userId' ";
	$resPD = executeQuery($sqlPD);
	if($rowPD = mysql_fetch_array($resPD))
	{
		@extract($rowPD);
	}
}
?>